<?php

namespace App\Http\Controllers;

use App\Framework\Enum\ManagerHandleLogLevelEnum;
use App\Framework\Helper\TimeHelper;
use App\Framework\Helper\WebHelper;
use App\Framework\Session\ManagerSession;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use LogFacade;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    const GENERAL = ManagerHandleLogLevelEnum::GENERAL;
    const WARNING = ManagerHandleLogLevelEnum::WARNING;
    const DANGER = ManagerHandleLogLevelEnum::DANGER;

    /**
     * 显示一条消息并返回到上一页
     * @param $message
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showMessageAndBack($message)
    {

        return view('shared.showMessageAndBack', ['message' => $message]);
    }

    /**
     * 显示一条消息并重定向到指定页面
     * @param $message
     * @param $rctUrl
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showMessageAndRedirect($message, $rctUrl)
    {
        return view('shared.showMessageAndRedirect', ['message' => $message, 'rctUrl' => $rctUrl]);
    }

    /**
     * 保存操作日志
     * @param $des
     * @param $level
     */
    public function storeAdminLog($des, $level = null)
    {
        if ($level == null) {
            $level = self::GENERAL;
        }

        $managerID = ManagerSession::getManagerID();
        $username = ManagerSession::getUsername();
        $clientIP = WebHelper::getClientIP();
        $now = TimeHelper::getNow();
        $url = request()->path();
        LogFacade::storeHandleLog($managerID, $username, $clientIP, $url, $level, $des, $now);
    }
}
